package com.kotlin.myapplication.mvp.presenter

import com.kotlin.myapplication.mvp.model.WeatherContract
import com.kotlin.myapplication.utils.Logger
import com.kotlin.myapplication.network.RetrofitUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

/**
 *@lil
 *@创建时间 2019/11/7 17:09
 *@描述
 */
class WeatherPresenter(val view: WeatherContract.view) : WeatherContract.Weather {

    /**
     * 订阅管理
     */
    var compositeDisposable: CompositeDisposable

    init {
        view.presenter = this
        compositeDisposable = CompositeDisposable()

    }

    override fun getWeather(date: String) {

//        var code = URLEncoder.encode(date, "UTF-8")
//        Logger.log(code)
        Logger.log(date)
        val disposable = RetrofitUtil
                .weatherService
                .getWeather(date, "793d00e1e829ecaa1bcdc29986854c86")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ result ->
                    view.showWeather(result)
                    Logger.log(result.toString())
                }, { error ->
                    view.error(error.message.toString())
//                    Logger.log(error.message.toString())

                })

        compositeDisposable.add(disposable)


    }

    /**
     * 开启订阅
     */
    override fun subscribe() {

    }

    /**
     * 关闭订阅
     */
    override fun unsubscribe() {
        compositeDisposable.clear()
    }


}